/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import java.util.Iterator;

public class CheckCommand extends FindCommand {

    /**
     * index of assertions in child list
     */
    private static final int ASSERTIONS_INDEX = 2;

    public Assertion assertionPara = null;
    
    public CheckCommand(final String name_, Location location_, Scope scope_, IntNode expects_, QualifiedNames assertions_) {
        super(name_, location_, scope_, expects_);
        addChild(assertions_);
    }


    public CheckCommand(final String name_, Scope scope_, IntNode expects_, QualifiedNames assertions_) {
        this(name_, Location.UNKNOWN, scope_, expects_, assertions_);
    }



    //GET AND SET METHODS

    /**
     * gets the assertions of this
     * @return the {@link QualifiedNames}
     */
    public QualifiedNames getAssertions() {
        return (QualifiedNames)childAt(ASSERTIONS_INDEX);
    }

    /** set the Assertions of this */
    public void setAssertions(QualifiedNames newChild_) {
        setChild(ASSERTIONS_INDEX, newChild_);
    }

    /**
     * accept an AST visitor
     * @param visitor_ the visitor
     */
    protected void acceptVisitor(ASTVisitor visitor_) {
        visitor_.visit(this);
    }

    /**
     * accept a returning AST visitor
     * @param visitor_ the visitor
     * @return the Object returned by the visitor
     */
    protected Object acceptReturnVisitor(ASTReturnVisitor visitor_) {
        return visitor_.visit(this);
    }

    public String nodeString() {
        StringBuffer ret = new StringBuffer("check ");
        Iterator assertions = getAssertions().getQualifiedNameIter();
        // should have 0 or 1 assertions here (for now)
        if (assertions.hasNext()) {
            QualifiedName curAssertion = (QualifiedName)assertions.next();
            ret.append(curAssertion.nodeString());
            alloy.util.Dbg.chk(!assertions.hasNext(), "more than one assertion in check command");
        }
        // add the scope and expects declarations if they exist
        ret.append(" " + ((TreeNode)getScope()).nodeString());
	if (!(getExpects().isEmpty())) {
            ret.append(" expect " + getExpects().nodeString());
        }
        return ret.toString();
    }

    public String toString()
    {
        return nodeString();
    }

    public FormulaPara getFormulaPara(ModuleInfo moduleInfo_) {
        if (assertionPara == null) {
            QualifiedNames assertionNames = getAssertions();
            if (!assertionNames.isLeaf()) { // assertion specified
                QualifiedName assertionName = (QualifiedName)assertionNames.getQualifiedNameIter().next();
                assertionPara = moduleInfo_.getAssertion(assertionName, "check");
            } else {
                // we don't support this construct for now
                alloy.util.Dbg.user(new alloy.util.Msg("must specify assertion to check", this));
            }
            
        }
        return assertionPara;
    }
}
